//岗位管理
function position_main() {
    return {
        id: 'BasicPosition_main',
        xtype: 'jgrid',
        clazz: BasicPosition,
        order: ['positionType->asc'],
        where: [['organId.id', '=', USER.areaId]],
        page: false,
        region: 'center',
        groupField: 'positionType',
        features: [{
            padding: '5px',
            groupHeaderTpl: "<b  class='label label-info'>分组：{[ DICS['positionType'][values.groupValue] ]} 共{rows.length}条记录</b>",
            ftype: 'grouping'
        }],
        top: [{
            name: 'positionType',
            op: '=',
            store: $.dic('stationType'),
            xtype: 'combo'
        }, {
            name: 'positionName',
            op: 'like',
            xtype: 'textfield'
        }, $.search, '->', {
            key: 'A',
            iconCls: ion.add,
            text: '添加岗位',
            handler: function () {
                BasicPosition_form().save({
                    grid: ['BasicPosition_main']
                });
            }
        }],
        columns: [{
            width: 40,
            renderer: $.iconRenderer('fa-play', '#D84A38')
        }, {
            width: 200,
            "name": "positionType",
            renderer: $.dicRenderer('stationType')
        }, {
            width: 200,
            "name": "positionName"
        }, {
            flex: 1,
            "name": "positionReson"
        }, {
            xtype: 'actioncolumn',
            width: 100,
            items: [{
                tooltip: '修改',
                iconCls: 'fa-pencil-square td-yellow x-fa',
                handler: function (grid, rowIndex, colIndex) {
                    var cmp = $('BasicPosition_main'), rec = grid.getStore().getAt(rowIndex);
                    cmp.select(rec);
                    CLICK_TEXT = '修改岗位';
                    BasicPosition_form().edit({
                        grid: ['BasicPosition_main']
                    });
                }
            }, {
                tooltip: '删除',
                iconCls: 'fa-close td-red x-fa',
                handler: function (grid, rowIndex, colIndex) {
                    var cmp = $('BasicPosition_main'), rec = grid.getStore().getAt(rowIndex);
                    cmp.select(rec);
                    cmp.deletes({
                        validate: [[BasicUser, 'positionId.id', '岗位{positionName}存在关联人员,请解除关联后再尝试.'],
                            [BasicRolePosition, 'positionId.id', '岗位{positionName}存在关联角色,请解除关联后再尝试.'],
                            [BasicPartTime, 'positionId.id', '岗位{positionName}存在兼职数据引用,请解除引用后再尝试.']]
                    });
                }
            }, {
                tooltip: '分配角色',
                iconCls: 'fa-flask td-black x-fa',
                handler: function (grid, rowIndex, colIndex) {
                    var cmp = $('BasicPosition_main'), rec = grid.getStore().getAt(rowIndex);
                    cmp.select(rec);
                    BasicPosition_main_distribution_role();
                }
            }]
        }]
    }
}

function BasicPosition_form() {
    return $({
        xtype: 'jform',
        clazz: BasicPosition,
        fieldsSize: 1,
        hides: [{
            name: 'organId.id',
            value: USER.areaId
        }],
        defaults: {
            lw: 90
        },
        validate: [['positionName = ? , organId.id = ?', 'positionName', '已存在的岗位名称']],
        fields: [{
            store: $.dic('stationType'),
            xtype: 'combo',
            req: true,
            "name": "positionType"
        }, {
            req: true,
            "name": "positionName"
        }, {
            "name": "positionReson",
            xtype: 'textarea'
        }]
    })
}

// 角色分配
function BasicPosition_main_distribution_role() {
    var sel = $('BasicPosition_main').select()[0];
    if (!sel) {
        return $.error('请选中一个岗位后再进行角色分配');
    }
    var name = sel.get('positionName'), id = sel.get('id');
    $.orm.getMtm({
        tagerFields: ['roleId'],
        clazz: BasicRolePosition,
        src: 'positionId',
        srcId: id,
        callback: function (data) {
            var form = $({
                width: 450,
                xtype: 'jform',
                fields: [{
                    text: '岗位名称',
                    value: name,
                    readOnly: true
                }, {
                    xtype: 'itemselector',
                    height: 250,
                    value: data.roleId,
                    displayField: 'roleName',
                    valueField: 'id',
                    id: 'basic_position_role_itemselector',
                    store: $.store({
                        clazz: BasicRole,
                        fields: ['roleName', 'id'],
                        where: [['roleType', '=', '3'], ['organId.id', '=', USER.areaId]]
                    }),
                    text: '岗位角色'
                }]
            });
            form.win({
                buttons: [{
                    text: '完成',
                    cls: 'green',
                    iconCls: 'fa-hand-o-down',
                    handler: function () {
                        $.orm.mtm({
                            clazz: BasicRolePosition,
                            src: 'positionId',
                            srcId: id,
                            infos: [{
                                tagerField: 'roleId',
                                ids: $('basic_position_role_itemselector').v().join(',')
                            }],
                            callback: function () {
                                $.show(Ext.String.format('岗位【{0}】的角色已成功分配', name));
                                form.win.close();
                            }
                        });
                    }
                }],
                title: Ext.String.format('为【{0}】分配角色', name)
            });
        }
    });
}
